package com.yltrcc.blog.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yltrcc.blog.generate.domain.NovelShelf;
import com.yltrcc.blog.generate.domain.QbCategory;
import com.yltrcc.blog.generate.domain.QbSubCategory;
import com.yltrcc.blog.generate.mapper.NovelShelfMapper;
import com.yltrcc.blog.generate.mapper.QbCategoryMapper;
import com.yltrcc.blog.generate.service.NovelShelfService;
import com.yltrcc.blog.generate.service.QbCategoryService;
import com.yltrcc.blog.generate.service.QbSubCategoryService;
import com.yltrcc.blog.model.model.ApiResponse;
import com.yltrcc.blog.model.model.BaseApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 企业微信 群推送APi
 */
@RestController
@RequestMapping(value = "/api/novel/shelf")
public class NovelShelfApiController {

    @Autowired
    private NovelShelfService novelShelfService;

    @Autowired
    private QbCategoryService qbCategoryService;

    @Autowired
    private QbSubCategoryService qbSubCategoryService;

    @Resource
    private NovelShelfMapper NovelShelfMapper;

    @Resource
    private QbCategoryMapper qbCategoryMapper;

    @RequestMapping(method = RequestMethod.POST, value = "/save")
    public BaseApiResponse save(@RequestParam(value = "novelName", defaultValue = "") String novelName,
                                @RequestParam(value = "novelIntro", defaultValue = "") String novelIntro ) {

        if (!StringUtils.hasText(novelName)) {
            return new BaseApiResponse();
        }
       try {
           //正常 回收站
           QbCategory qbCategory = new QbCategory();
           qbCategory.setCategoryName(novelName);
           qbCategory.setCategoryDescribe(novelName);
           qbCategory.setIsProject(1);
           qbCategoryService.save(qbCategory);

           QbCategory qbCategory1 = new QbCategory();
           qbCategory1.setCategoryName("概览");
           qbCategory1.setCategoryDescribe("概览");
           qbCategory1.setUpperCategoryId(qbCategory.getCategoryId());
           qbCategoryService.save(qbCategory1);

           QbCategory qbCategory2 = new QbCategory();
           qbCategory2.setCategoryName("概览");
           qbCategory2.setCategoryDescribe("概览");
           qbCategory2.setUpperCategoryId(qbCategory1.getCategoryId());
           qbCategoryService.save(qbCategory2);

           QbSubCategory qbSubCategory = new QbSubCategory();
           qbSubCategory.setCategoryId(qbCategory2.getCategoryId());
           qbSubCategory.setName("概览");
           qbSubCategory.setCategoryDescribe("概览");
           qbSubCategoryService.save(qbSubCategory);

           //正常 回收站
           NovelShelf NovelShelf = new NovelShelf();
           NovelShelf.setNovelName(novelName);
           NovelShelf.setNovelIntro(novelIntro);
           NovelShelf.setCreateTime(new Date());
           NovelShelf.setUpdateTime(new Date());
           NovelShelf.setUpperCategoryId(qbCategory.getCategoryId());
           novelShelfService.save(NovelShelf);
       }catch (DuplicateKeyException e) {
           return new BaseApiResponse(500, 500, "不可保存相同内容！！！");
       }catch (Exception e) {
           return new BaseApiResponse(500, 500, "未知异常，请联系管理员！！！");
       }

        return new BaseApiResponse();
    }

    /**
     * 显示所有小记
     */
    @RequestMapping(method = RequestMethod.GET, value = "/get")
    public ApiResponse<NovelShelf> get(@RequestParam(value = "page", defaultValue = "1") int page,
                                    @RequestParam(value = "limit", defaultValue = "8") int limit) {

        QueryWrapper<NovelShelf> wrapper = new QueryWrapper<>();
        //update_time 降序
        wrapper.orderByDesc("update_time");
        Page<NovelShelf> novelShelfPage = novelShelfService.page(new Page<>(page, limit), wrapper);

        return new ApiResponse<>(novelShelfPage.getRecords());
    }

    @RequestMapping(method = RequestMethod.POST, value = "/updateContent")
    public BaseApiResponse updateContent(@RequestParam(value = "id", defaultValue = "0") Integer id,
                                         @RequestParam(value = "novelName", defaultValue = "") String novelName,
                                         @RequestParam(value = "novelIntro", defaultValue = "") String novelIntro) {

        if (!StringUtils.hasText(novelName) || id==0) {
            return new BaseApiResponse();
        }
        try {
            NovelShelf novelShelf = novelShelfService.getById(id);
            if (!StringUtils.isEmpty(novelName)) {
                novelShelf.setNovelName(novelName);
            }
            if (!StringUtils.isEmpty(novelIntro)) {
                novelShelf.setNovelIntro(novelIntro);
            }
            if (null == novelShelf.getUpperCategoryId() || 0 ==  novelShelf.getUpperCategoryId()) {
                //正常 回收站
                QbCategory qbCategory = new QbCategory();
                qbCategory.setCategoryName(novelName);
                qbCategory.setCategoryDescribe(novelName);
                qbCategory.setIsProject(1);
                qbCategoryService.save(qbCategory);

                QbCategory qbCategory1 = new QbCategory();
                qbCategory1.setCategoryName("概览");
                qbCategory1.setCategoryDescribe("概览");
                qbCategory1.setUpperCategoryId(qbCategory.getCategoryId());
                qbCategoryService.save(qbCategory1);

                QbCategory qbCategory2 = new QbCategory();
                qbCategory2.setCategoryName("概览");
                qbCategory2.setCategoryDescribe("概览");
                qbCategory2.setUpperCategoryId(qbCategory1.getCategoryId());
                qbCategoryService.save(qbCategory2);

                QbSubCategory qbSubCategory = new QbSubCategory();
                qbSubCategory.setCategoryId(qbCategory2.getCategoryId());
                qbSubCategory.setName("概览");
                qbSubCategory.setCategoryDescribe("概览");
                qbSubCategoryService.save(qbSubCategory);

                novelShelf.setUpdateTime(new Date());
                novelShelf.setUpperCategoryId(qbCategory.getCategoryId());
            }
            novelShelf.setUpdateTime(new Date());
            NovelShelfMapper.updateById(novelShelf);
            //更新 分类 ID 名字
            QbCategory qbCategory = qbCategoryService.getById(novelShelf.getUpperCategoryId());
            qbCategory.setCategoryName(novelShelf.getNovelName());
            qbCategory.setCategoryDescribe(novelShelf.getNovelIntro());

            qbCategoryMapper.updateById(qbCategory);
        }catch (Exception e) {
            return new BaseApiResponse(500, 500, "未知异常，请联系管理员！！！");
        }

        return new BaseApiResponse();
    }

}
